import sedh.project.*;

class BootStrap {
	
	def springSecurityService
	
    def init = { servletContext ->
		
		def userRole = getOrCreateRole("ROLE_USER")
		def adminRole = getOrCreateRole("ROLE_ADMIN")
		
		def adminUser = new User(
			username: "admin",
			password: "admin",
			enabled: true,
			nome: "Administrador",
			municipio: "Joao Pessoa",
			papel: sedhproject.Papel.USUARIO,
			telefone: "88888888",
			email: "admin@mail.org",
			rg: "88888888" ).save(failOnError: true)
		
		SecUserSecRole.create(adminUser, adminRole)
		
		def normalUser = new User(
			username: "usuario",
			password: "usuario",
			enabled: true,
			nome: "Usuario Comum",
			municipio: "Campina Grande",
			papel: sedhproject.Papel.TECNICO,
			telefone: "1111111111",
			email: "usuario@mail.org",
			rg: "11111111").save(failOnError: true)
		
		
		SecUserSecRole.create(normalUser, userRole)
		
		def cg = new Municipio(nome: "Campina Grande", habitantes: 387.643 ).save(failOnError: true)
		def pt = new Municipio(nome: "Patos", habitantes: 100.695 ).save(failOnError: true)
		def sp = new Municipio(nome: "Sape", habitantes: 50.151 ).save(failOnError: true)
		def so = new Municipio(nome: "Sousa", habitantes: 65.807 ).save(failOnError: true)
		
		def creas = new Servico(descricao:"Centro de Referencia Especializada em Assistencia Social", nome:"CREAS").save(failOnError: true)
		def cras = new Servico(descricao:"Centro de Referencia em Assistencia Social", nome:"CRAS").save(failOnError: true)
		
		def bf = new Programa(descricao:"Programa de assistencia a familias pobres", nome:"Bolsa Familia").save(failOnError: true)
		def pj = new Programa(descricao:"Programa de capacitacao profissional aos jovens", nome:"PROJOVEM").save(failOnError: true)
		
		
    }
    
	def destroy = {
    }
	
	private getOrCreateRole(name) {
		def role = SecRole.findByAuthority(name)
		if (!role) role = new SecRole(authority: name).save()
		if (!role)  println "Unable to save role ${name}"
		return role
	}
}
